package org.ossean.gather.process;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.ossean.gather.model.Configure;
import org.ossean.gather.sourceDao.GatherDao;
import org.ossean.gather.targetDao.PointerDao;
import org.ossean.gather.targetDao.TargetDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class GatherProcess {
	private static Logger logger = Logger.getLogger(GatherProcess.class);
	public static Map<String,Boolean> gatherState = new HashMap<String,Boolean>(); //
	public static Set<String> urlMd5Set = new HashSet<String>(); //所有gather数据url_md5放入内存
	private Configure conf;
	private ExecutorService pool = Executors.newFixedThreadPool(20);
	@Resource
	private GatherDao gatherDao;
	@Resource
	private TargetDao targetDao;
	@Resource
	private PointerDao pointerDao;
    
	public void start(String configureName){
		while(true){
			conf = new Configure(configureName);
			//urlMd5Set = targetDao.initUrlMd5Set(conf.getTargetTableName());
			for(String sourceTableName : conf.getSourceTableNameList()){
				Boolean state = gatherState.get(sourceTableName);
				if( state == null || state == false){
					gatherState.put(sourceTableName, true);
				}
				else if(state == true){
					continue;
				}
				//GatherThread gatherThread = (GatherThread)AppContext.appContext.getBean("gatherThread");
				GatherThreadNew gatherThreadNew = (GatherThreadNew)AppContext.appContext.getBean("gatherThreadNew");
				gatherThreadNew.setParameters(conf,sourceTableName);
				pool.execute(gatherThreadNew);
			}
			try {
				logger.info(".......sleeping......");
				Thread.sleep(20000);
			} catch (InterruptedException e) {
				logger.error(e);
			}
			System.gc();  //手动垃圾回收
		}
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		//获取传递的参数
		String configureName = "";
	    if (args.length != 0) {
	        configureName = args[0].toString();
	    } else {
	        logger.error("404 configure!");
	        configureName = "relative_memos";
	        //configureName = "gather_projects";
	        //return;
	    }
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		GatherProcess main = applicationContext.getBean(GatherProcess.class);
		main.start(configureName);
	}

}
